package cn.hesion.thread;

public class DeadLockDemo {
    private static Object resource1 = new Object();//资源 1
    private static Object resource2 = new Object();//资源 2

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized(resource1){
                System.out.println(Thread.currentThread()+"get Resource1");
                try  {
                    Thread.sleep(1000);
                } catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+"wait Resource2");
                synchronized(resource2){
                    System.out.println(Thread.currentThread()+"get Resource2");
                }
            }
        },"线程1").start();

        new Thread(() -> {
            synchronized(resource1){
                System.out.println(Thread.currentThread()+"get Resource1");
                try  {
                    Thread.sleep(1000);
                } catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+"wait Resource2");
                synchronized(resource2){
                    System.out.println(Thread.currentThread()+"get Resource2");
                }
            }
        },"线程2").start();

    }
}
